package com.shengsheng.expschedule.service.expschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shengsheng.expschedule.service.expschedule.entity.Class;
import com.shengsheng.expschedule.service.expschedule.entity.Major;
import com.shengsheng.expschedule.service.expschedule.mapper.ClassMapper;
import com.shengsheng.expschedule.service.expschedule.mapper.MajorMapper;
import com.shengsheng.expschedule.service.expschedule.service.MajorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 专业表 服务实现类
 * </p>
 *
 * @author yuan
 * @since 2021-10-02
 */
@Service
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements MajorService {

    @Autowired
    private ClassMapper classMapper;

    @Override
    public List<Major> getListOrderBySort() {

        QueryWrapper<Major> majorQueryWrapper = new QueryWrapper<>();
        majorQueryWrapper.orderByAsc("sort")
                .orderByAsc("gmt_create");
        return this.list(majorQueryWrapper);
    }

    @Override
    public boolean removeMajorById(String id) {

        // 先判断此专业下是否有班级
        QueryWrapper<Class> classQueryWrapper = new QueryWrapper<>();
        classQueryWrapper.eq("major_id", id);
        List<Class> classList = classMapper.selectList(classQueryWrapper);
        if (classList.size() > 0) return false;
        return this.removeById(id);
    }
}
